/*
 * @Descripttion: 护士将订单状态改成 '完成交易'
 * @version: 1.0
 * @Author: 自由如风
 * @Date: 2021-05-06 13:12:28
 * @LastEditors: 自由如风
 * @LastEditTime: 2021-05-06 13:18:10
 */

const DB = require('../../lib/DB')
const { Success, Fail } = require('../../lib/Response');
module.exports = async function (ctx, next) {
    try {
        const buy_medicine = await DB.models.buy_medicine.findOne({
            where: { id: ctx.request.body.id }, include: [
                {
                    model: DB.models.user,
                    as: 'nurse'
                },
                {
                    model: DB.models.patient,
                    as: 'patient'
                },
                {
                    model: DB.models.medicine_lib,
                    as: 'medicine_lib'
                },
            ]
        });
        if (buy_medicine.state != '等待患者取药') {
            throw new Error("该订单目前状态不能转换成'交易关闭'状态")
        }
        buy_medicine.state = '交易关闭'
        buy_medicine.memo = ctx.request.body.memo
        buy_medicine.nurse_id = ctx.session.nurse.id

        await buy_medicine.save()
        ctx.body = new Success(buy_medicine.toJSON())
        await next();
    } catch (e) {
        ctx.body = new Fail(e.message)
    }
}
